<?php

namespace App\Model;

use EasySwoole\DDL\Enum\Character;
use EasySwoole\DDL\Enum\Engine;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\ORM\Utility\Schema\Table;
use Throwable;

class AdjacentSchoolModel extends AbstractModel
{
    protected $tableName = 't_adjacent_school';
    protected $autoTimeStamp = true; // 自动写入create_time和update_time 这两个字段是int

    /**
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getAdjacent($where)
    {
        $where['is_deleted'] = 0;
        return $this->order('id', 'desc')->get($where);
    }

    /**
     * @throws Throwable
     * @throws Exception
     */
    public function addAdjacent($where)
    {
        $this->data($where, false);
        return $this->save();
    }

    public function schemaInfo(bool $isCache = true): Table
    {
        $table = new Table($this->tableName);

        $table->setIfNotExists();
        $table->setTableEngine(Engine::INNODB);
        $table->setTableCharset(Character::UTF8MB4_GENERAL_CI);
        $table->setTableComment('四点半课堂报名表');

        $table->colInt('id')->setIsAutoIncrement()->setIsPrimaryKey(true)->setColumnComment("ID");
        $table->colInt('uid', 16)->setDefaultValue(0)->setIsNotNull()->setColumnComment('用户id');
        $table->colInt('nid', 16)->setDefaultValue(0)->setIsNotNull()->setColumnComment('服务项目');
        $table->colVarChar('house', 128)->setDefaultValue('')->setColumnComment('房屋信息');
        $table->colVarChar('child_name', 64)->setDefaultValue('')->setColumnComment('孩子姓名');
        $table->colVarChar('child_grade', 64)->setDefaultValue('')->setColumnComment('孩子班级');
        $table->colVarChar('child_hobby', 64)->setDefaultValue('')->setColumnComment('孩子爱好');
        $table->colInt('child_age', 3)->setDefaultValue(0)->setColumnComment('孩子年龄');
        $table->colVarChar('name', 64)->setDefaultValue('')->setColumnComment('联系人');
        $table->colVarChar('tel', 11)->setDefaultValue('')->setColumnComment('联系电话');
        $table->colVarChar('name1', 64)->setDefaultValue('')->setColumnComment('临时联系');
        $table->colVarChar('tel1', 11)->setDefaultValue('')->setColumnComment('临时电话');
        $table->colInt('start_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('开始时间');
        $table->colInt('end_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('结束时间');
        $table->colTinyInt('status', 1)->setDefaultValue(0)->setIsNotNull()->setColumnComment('状态：0.待审核，1.进行中,2.待支付，-1.已取消');
        $table->colTinyInt('is_deleted', 1)->setDefaultValue(0)->setIsNotNull()->setColumnComment('是否删除：0.未删除，1.已删除');
        $table->colInt('create_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('创建时间戳');
        $table->colInt('update_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('更新时间戳');

        $table->indexNormal('uid_index', 'uid');
        $table->indexNormal('nid_index', 'nid');
        $table->indexNormal('house_index', 'house');
        $table->indexNormal('start_time_index', 'start_time');
        $table->indexNormal('name_index', 'name');
        $table->indexNormal('tel_index', 'tel');
        $table->indexNormal('status_index', 'status');
        $table->indexNormal('child_name_index', 'child_name');

        return $table;
    }
}